using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using RikMigrations.Providers;
using System.IO;

namespace RikMigrations.Tests
{
	public class SchemaClassTests
	{
		protected DbProvider db;
		public SchemaClassTests(DbProvider db)
		{
			this.db = db;
		}

		[SetUp]
		public virtual void Setup()
		{
			db.CreateTransaction();
		}

		[TearDown]
		public virtual void TearDown()
		{
			db.RollbackTransaction();
		}

		#region Illegal Names
		[Test]
		public void IllegalTableName()
		{
			Table t = db.AddTable("From");
			t.AddColumn("ID", typeof(int)).PrimaryKey();
			t.Save();

			t = db.AddTable("To");
			t.AddColumn("ID", typeof(int)).PrimaryKey();
			t.Save();
		}

		[Test]
		public void IllegalTableNameDrop()
		{
			Table t = db.AddTable("From");
			t.AddColumn("ID", typeof(int)).PrimaryKey();
			t.Save();
			db.DropTable("From");

			t = db.AddTable("To");
			t.AddColumn("ID", typeof(int)).PrimaryKey();
			t.Save();
			db.DropTable("To");
		}

		[Test]
		public void IllegalColumnNames()
		{
			Table t = db.AddTable("TestTable");
			t.AddColumn("ID", typeof(int)).PrimaryKey();
			t.AddColumn("To", typeof(int));
			t.AddColumn("From", typeof(int));
			t.Save();
		}

		[Test]
		public void IllegalColumnNamesExisting()
		{
			Table t = db.AddTable("TestTable");
			t.AddColumn("ID", typeof(int)).PrimaryKey();
			t.Save();
			t.AddColumn("To", typeof(int));
			t.AddColumn("From", typeof(int));
			t.Save();
		}

		[Test]
		public void IllegalColumnNamesDrop()
		{
			Table t = db.AddTable("TestTable");
			t.AddColumn("ID", typeof(int)).PrimaryKey();
			t.AddColumn("To", typeof(int));
			t.AddColumn("From", typeof(int));
			t.Save();
			t.DropColumn("To");
			t.DropColumn("From");
		}

		[Test]
		public void IllegalConstraintNames()
		{
			Table t = db.AddTable("TestTable");
			t.AddColumn("ID", typeof(int));
			t.AddConstraint("From", ConstraintType.PrimaryKey, "ID");
			t.Save();
		}

		[Test]
		public void IllegalConstraintNamesExisting()
		{
			Table t = db.AddTable("TestTable");
			t.AddColumn("ID", typeof(int)).NotNull();
			t.Save();
			t.AddConstraint("From", ConstraintType.PrimaryKey, "ID");
			t.Save();
		}

		[Test]
		public void IllegalConstraintNamesDrop()
		{
			Table t = db.AddTable("TestTable");
			t.AddColumn("ID", typeof(int));
			t.AddConstraint("From", ConstraintType.PrimaryKey, "ID");
			t.Save();
			t.DropConstraint("From");
		}
		#endregion
	}

	[TestFixture]
	public class MssqlSchemaClassTests : SchemaClassTests
	{
		public MssqlSchemaClassTests()
			: base(new MssqlProvider())
		{
		}

		[SetUp]
		public override void Setup()
		{
			db.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
			base.Setup();
		}
	}

#if SQLITE
	[TestFixture]
	public class SqliteSchemaClassTests : SchemaClassTests
	{
		string dbPath;

		public SqliteSchemaClassTests()
			: base(new SqliteProvider())
		{
		}

		[SetUp]
		public override void Setup()
		{
			dbPath = Path.GetTempFileName();
			db.ConnectionString = string.Format("Data Source={0}", dbPath);

			base.Setup();
		}

		[TearDown]
		public override void TearDown()
		{
			base.TearDown();
			File.Delete(dbPath);
		}
	}
#endif
}